package com.qysoft.modules.genTable.action;


import com.jfinal.aop.Before;
import com.qysoft.modules.genTable.entity.GenTable;
import com.qysoft.modules.genTable.entity.GenTableColumn;
import com.qysoft.modules.genTable.service.GenTableService;
import com.qysoft.modules.genTable.util.GenUtils;
import com.qysoft.rapid.actions.JsonResultInterceptor;
import com.qysoft.rapid.actions.RapidAction;
import com.qysoft.rapid.core.RapidDbConnTx;
import com.qysoft.rapid.dao.mybatis.RapidDao;
import com.qysoft.rapid.dao.mybatis.common.CommonDao;
import com.qysoft.rapid.domain.Bean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GenTableAction extends RapidAction {

	public void index() throws Exception{

		bindCurrentRyxxToPage();
		List<Bean> tableList = RapidDao.selectListByXml("findTableList");
		setAttr("tableList",tableList);
		renderJsp("genTable.jsp");
	}

	/**
	 * 跳转到配置业务表页面
	 * @throws Exception
	 */
	public void addGenTable()throws Exception{

		String tableName = getPara("name");
		String comments = getPara("comments");
		GenTable genTable = new GenTable();
		genTable.setName(tableName);
		genTable.setComments(comments);
		genTable = GenTableService.getTableFormDb(genTable);
		setAttr("genTable",genTable);
		setAttr("javaTypeList", GenUtils.getConfig().getJavaTypeList());
		renderJsp("addGentable.jsp");
	}

	/**
	 * 保存数据表
	 * @throws Exception
	 */
	@Before({JsonResultInterceptor.class,RapidDbConnTx.class})
	public void saveTable()throws Exception{

		Bean genTableBean= getBean();
		String tableName = genTableBean.get("name").toString();
		String comments = genTableBean.get("comments").toString();
		String class_name = genTableBean.get("class_name").toString();
		Integer row = Integer.parseInt(genTableBean.get("row").toString());

		Map map = new HashMap();
		map.put("tableName",tableName);
		GenTable bean = CommonDao.selectOneByXml("query_genTable_one",map);
		if(bean != null){
			setJson("fail");
			return;
		}
		GenTable genTable = new GenTable();
		genTable.setName(tableName);
		genTable.setClassName(class_name);
		genTable.setRemarks(comments);
		genTable.setComments(comments);
		genTable.setCreateBy(getCurrentRyxx().getId());
		genTable.setUpdateBy(getCurrentRyxx().getId());
		genTable = GenTableService.getTableFormDb(genTable);
		int genTable_insert = CommonDao.insertByXml("genTable_insert", genTable);
		GenTable genTableT = CommonDao.selectOneByXml("query_genTable_one",map);
		if(genTable_insert == 1){
			for (int i = 0; i < row  ; i++) {
				GenTableColumn column = new GenTableColumn();
				column.setName(genTableBean.get("dataArray["+i+"][name]").toString());
				column.setComments(genTableBean.get("dataArray["+i+"][comments]").toString());
				column.setJdbcType(genTableBean.get("dataArray["+i+"][jdbcType]").toString());
				column.setIsNull(genTableBean.get("dataArray["+i+"][isNull]").toString());
				column.setJavaType(genTableBean.get("dataArray["+i+"][javaType]").toString());
				column.setJavaField(genTableBean.get("dataArray["+i+"][javaField]").toString());
				column.setGenTable(genTableT.getId());
				CommonDao.insertByXml("genTableColumn_insert", column);
			}
		}
		renderJsp("addGentable.jsp");
	}

	/**
	 * 删除业务表
	 * @throws Exception
	 */
	@Before(JsonResultInterceptor.class)
	public void delete()throws Exception{

		String id = getPara("id");
		RapidDao.deleteByXml("genTable_delete",id);
		RapidDao.deleteByXml("genTableColumn_delete",id);
	}

}
